package zt.song.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import zt.song.entity.AccountUser;
import zt.song.entity.SysUser;
import zt.song.mapper.SysUserMapper;

import java.util.TreeSet;

/**
 * @Author 宋伟宁
 * @Date 2023/11/22
 * @Version 1.0
 **/
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("----------------------------------------"+username);
        QueryWrapper<SysUser> queryWrapper =new QueryWrapper<>();
           queryWrapper.eq("username",username);
        SysUser sysUser = this.sysUserMapper.selectOne(queryWrapper);
        if( sysUser == null){
             throw  new UsernameNotFoundException("账号不存在");
        }
        return new AccountUser(sysUser.getId(), sysUser.getUsername(),
                sysUser.getPassword(), new TreeSet<>());
    }
}
